﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClientAppViaWebService.Forms;
using System.Windows.Forms;
using ClientAppViaWebService.DivisionsLogicServiceRef;

namespace ClientAppViaWebService.Utility
{
    public static class ReflectionLogic
    {
        public static TObjectEditForm CreateFormByName(string formName)
        {
            TObjectEditForm baseObjectForm = null;
            Type formType = Type.GetType(formName);

            if (formType == null)
                throw new Exception("Form of type: '" + formName + "' not found!");

            object newForm = Activator.CreateInstance(formType);


            if (newForm is TObjectEditForm)
                baseObjectForm = (TObjectEditForm)newForm;
            else
                throw new Exception("Error ocurred while casting form to base form!");
            return baseObjectForm;
        }

        public static Form CreateAnyFormByName(string formName)
        {
            Form baseObjectForm = null;
            Type formType = Type.GetType(formName);

            if (formType == null)
                throw new Exception("Form of type: '" + formName + "' not found!");

            object newForm = Activator.CreateInstance(formType);

            if (newForm is Form)
                baseObjectForm = (Form)newForm;
            else
                throw new Exception("Error ocurred while casting form to a Form!");
            return baseObjectForm;
        }

        public static TObjectEditForm CreateFormByTObjectClassId(string databaseName, int class_id)
        {           
            BaseLogicServiceRef.BaseLogicService service = new ClientAppViaWebService.BaseLogicServiceRef.BaseLogicService();
            
            string formName = "ClientAppViaWebService.Forms."+
            
            service.SelectClassFormNameByTobjectDBandClass_Id(databaseName, class_id);

            return formName != "ClientAppViaWebService.Forms." ? CreateFormByName(formName) : new TObjectEditForm();
        }
    }
}
